
<!-- 
接受参数: 
对话框标题
单据表格列
物品表格列
原单名称: 销售订单
返回的参数: 原单数据
-->
<template>
  <div>
    <!-- 原单生成按钮和表格 -->
    <a-button type="primary" style="margin-top: 20px" @click="showModal">
      <a-icon type="plus" />
      根据源单生成
    </a-button>
    <a-modal
      :title="ModalTitle"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      width="70%"
    >
      <!-- 搜索表单---查询条件 -->
      <a-form v-bind="layout">
        <a-row>
          <!-- 销售订单显示 -->
          <a-col
            :xl="8"
            :lg="8"
            :md="12"
            :sm="24"
            :xs="24"
            v-if="saleOrderShowType"
          >
            <a-form-item label="销售部门">
              <a-select
                showSearch
                style="width: 100%"
                v-model="searchForm.salesDepartment"
              >
                <a-select-option
                  v-for="item in saleDepartmentList"
                  :key="item.value"
                  :value="item.title"
                  >{{ item.title }}</a-select-option
                >
              </a-select>
            </a-form-item>
          </a-col>
          <!-- 公共部分 -->
          <a-col :xl="10" :lg="10" :md="12" :sm="24" :xs="24">
            <a-form-item label="时间段">
              <a-range-picker
                v-model="searchForm.rangeDate"
                @change="getRangeTime"
              />
            </a-form-item>
          </a-col>

          <!-- 按钮 -->
          <a-col
            :xl="6"
            :lg="6"
            :md="24"
            :sm="24"
            :xs="24"
            :style="{ textAlign: 'right' }"
            ><div style="margin-top: 10px">
              <a-button type="primary" @click="Search()">查询</a-button>
              <a-button style="margin-left: 8px" @click="Reset()"
                >重置</a-button
              >
            </div>
          </a-col>
        </a-row>
        <a-row> </a-row>
      </a-form>

      <a-divider />

      <!-- 备货通知单、销售订单等单据 -->
      <a-table
        style="margin-top: 20px"
        :columns="orderColumns"
        :data-source="orderDataSource"
        :row-selection="{
          selectedRowKeys: orderSelectedRowKeys,
          onChange: orderOnSelectChange,
        }"
      >
      </a-table>

      <div :style="{ textAlign: 'right' }">
        <a-button type="primary" @click="Search()">查询</a-button>
        <a-button style="margin-left: 8px" @click="Empty()">清空</a-button>
      </div>

      <a-divider />
      <!-- 单据中的物品 -->
      <a-table
        style="margin-top: 10px"
        :columns="listColumns"
        :data-source="ListDataSource"
        :scroll="{ x: 1500 }"
        :row-selection="{
          selectedRowKeys: listSelectedRowKeys,
          onChange: listOnSelectChange,
        }"
      >
      </a-table>

      <!-- 备货通知单物品信息 -->
    </a-modal>
  </div>
</template>

<script>
import { CommonAjax } from "@/services/modules/common";
const comHttp = new CommonAjax(); // 公用接口

export default {
  props: {
    // 对话框标题
    ModalTitle: {
      type: String,
      required: true,
    },

    // 表单列
    orderColumns: {
      type: Array,
      required: true,
    },
    // 列表列
    listColumns: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      visible: false,

      searchForm: {
        rangeDate: null, // 时间范围
        departmentName: "",
        departmentId: "",

        // 备货通知单部门
        applicationDept: "", // 部门名称
        departmentNo: "", // 部门编号
      },

      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 15 },
      },

      // 订单左侧选择框
      orderSelectedRowKeys: [], // 订单选择
      orderDataSource: [{ key: 1 }],

      // 列表左侧选择框
      listSelectedRowKeys: [], // 列表选择
      ListDataSource: [{ key: 1 }],

      startTime: "", // 开始时间
      endTime: "", // 结束时间

      // 销售订单独有！！！！
      saleOrderShowType: false,
      saleDepartmentList: [], // 销售部门列表

      // 备货通知单独有！！！！！
      stockNoticeShowType: false,
      deparmentList: [], // 部门列表
      ApplyPersonList: [], // 出库申请人列表
    };
  },

  methods: {
    // 表格选择事件,
    orderOnSelectChange(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.orderSelectedRowKeys = selectedRowKeys;
    },
    // 列表选择事件
    listOnSelectChange(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.listSelectedRowKeys = selectedRowKeys;
    },

    // ---------- 公用函数 ----------
    // 时间段
    getRangeTime(date, dateString) {
      this.startTime = dateString[0];
      this.endTime = dateString[1];
      console.log(this.startTime);
      console.log(this.endTime);
    },
    // 获取当前查询路径
    getQueryPath() {
      let queryPath = "?pageNo=";
      return queryPath;
    },
    // 搜索
    Search() {
      console.log("输出搜索表单", this.searchForm);
      if (this.ModalTitle == "备货通知单") {
        let queryPath = this.getQueryPath();
        this.getStockNoticeList(queryPath);
      }
    },

    // 重置
    Reset() {
      this.searchForm = {};
      this.startTime = "";
      this.endTime = "";
    },
    // 展示对话框
    showModal() {
      console.log("");
      // 如果传递过来的是销售订单,样式更换为::::::
      if (this.ModalTitle == "销售订单生成备货通知") {
        this.saleOrderShowType = true;
        this.getSaleDepartmentList();
      } else if (this.ModalTitle == "备货通知单") {
        this.stockNoticeShowType = true;
        this.getDeparmentTree(); // 获取部门树
        this.getUserIdByRoleName(); // 获取出库申请人
      }
      this.visible = true;
    },
    // 提交
    handleOk() {
      console.log("提交");
      this.visible = false;
      // console.log("下方物品数据:::", this.ListDataSource);
      // console.log("下方表格数据筛选结果:::", this.listSelectedRowKeys);

      // !!!!! 筛选出选中的数据
      let goodsLists = [];
      for (let i = 0; i < this.listSelectedRowKeys.length; i++) {
        let target = this.ListDataSource.filter(
          (item) => item.key === this.listSelectedRowKeys[i]
        )[0];
        goodsLists.push(target);
      }
      console.log("筛选的选中数据，要传递给父组件::::", goodsLists);
    },
    // 清空
    Empty() {
      this.ListDataSource = [];
      console.log("清空");
    },
    // 取消
    handleCancel() {
      this.visible = false;
    },

    // 根据业务需求,自己添加相应的样式和函数
    // ----------- 用于销售订单独有的 --------------
    // 销售部门
    getSaleDepartmentList() {
      this.saleDepartmentList = [];
      comHttp.getSaleDepartment().then((res) => {
        console.log(res);
        for (let i = 0; i < res.result.length; i++) {
          let data = {
            value: res.result[i].value,
            title: res.result[i].title,
          };
          this.saleDepartmentList.push(data);
        }
      });
    },

    // 部门按钮触发
    DepartmentChange(value, option) {
      this.searchForm.departmentId = option.key;
    },

    // 获取销售订单列表

    // ----------- 用于备货通知单独有的 -----------
    // 获取部门列表
    getDeparmentTree() {
      comHttp.getDepartmentList().then((res) => {
        // let tree = this.resetTree(res.result);
        // console.log("获取部门树::::",tree);
        this.deparmentList = res.result;
        this.resetTree(this.deparmentList);
      });
    },
    // 树节点的 value值 变成 title,为了加上搜索框.
    resetTree(Tree) {
      Tree.forEach((item) => {
        item.value = item.title;
        if (item.children) {
          this.resetTree(item.children);
        }
      });
    },
    // 部门下拉框选择
    changeTreeNode(value, label, extra) {
      this.searchForm.departmentNo = extra.triggerNode.eventKey;
      console.log("选择的值::::", this.searchForm.departmentNo);
    },
    // 获取人员信息
    getUserIdByRoleName() {
      const roleName = "出库申请人员";
      let queryPath = "?roleName=" + roleName;
      comHttp.getUserRoleNameByUserNameId(queryPath).then((res) => {
        // console.log(res);
        for (let i = 0; i < res.result.length; i++) {
          let data = {
            userName: res.result[i].userName,
            userId: res.result[i].userId,
            workNo: res.result[i].workNo,
          };
          this.ApplyPersonList.push(data);
        }
        console.log(this.ApplyPersonList);
      });
    },
    // 申请人赋值
    getApplicantId(value, options) {
      this.searchForm.applicantId = options.key;
    },
    // 获取备货通知单列表
    getStockNoticeList() {
      console.log("获取当前备货通知单列表:::");
    },
  },
};
</script>

<style></style>
